const { type } = require("jquery");

define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index: async function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'product/product/index',
                    add_url: 'product/product/add',
                    edit_url: 'product/product/edit',
                    del_url: 'product/product/del',
                    type_url: 'product/type/index', // 获取分类列表接口
                    unit_url: 'product/unit/index', // 获取单位列表接口
                    table: 'product', // 表名
                },
                fixedColumns: true,
                fixedRightNumber: 1,
            });

            // 获取到table标签的id
            var table = $("#table")

            // 获取分类列表
            var typeList = [];
            await $.getJSON($.fn.bootstrapTable.defaults.extend.type_url, function (data) {
                $.each(data.rows, function (index, type) {
                    typeList.push(type.name);
                });
            })

            // 获取单位列表
            var unitList = [];
            await $.getJSON($.fn.bootstrapTable.defaults.extend.unit_url, function (data) {
                $.each(data.rows, function (index, unit) {
                    unitList.push(unit.name);
                });
            })

            // 初始化表格
            table.bootstrapTable({
                // 当访问列表发起ajax请求数据
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                // 主键
                pk: 'id',
                // 设置默认排序
                sortName: 'id',
                sortOrder: 'desc',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id'), sortable: true, operate: false },
                        { field: 'name', title: __('Pname'), operate: 'LIKE' },
                        { field: 'thumbs_text', title: __('Pimage'), formatter: Table.api.formatter.image, operate: false },
                        { field: 'price', title: __('Pprice'), sortable: true, operate: false },
                        {
                            field: 'type.name', title: __('Ptype'),
                            searchList: typeList,
                            formatter: Table.api.formatter.flag,
                            operate: 'FIND_IN_SET',
                        },
                        {
                            field: 'unit.name', title: __('Punit'),
                            searchList: unitList,
                            formatter: Table.api.formatter.flag,
                            operate: 'FIND_IN_SET',
                        },
                        {
                            field: 'status', title: __('Pstatus'),
                            searchList: {
                                '0': __('下架'),
                                '1': __('上架'),
                            },
                            formatter: Table.api.formatter.flag,
                            custom: { "0": "warning", "1": "success" },
                            operate: 'FIND_IN_SET',
                        },
                        {
                            field: 'flag', title: __('Pflag'),
                            searchList: {
                                '1': __('新品'),
                                '2': __('热销'),
                                '3': __('推荐'),
                            },
                            formatter: Table.api.formatter.flag,
                            custom: { "2": "danger", "1": "success", '3': 'info' },
                            operate: 'FIND_IN_SET',
                        },
                        { field: 'createtime', title: __('PcreateTime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            })

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        // 添加
        add: function () {
            // 为表单绑定事件
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        del: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    }


    return Controller
})